<template>
  <div>
    <MyTable :arr="list">
      <template v-slot:title>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template v-slot="scoped">
        <td>{{ scoped.row.id }}</td>
        <td>{{ scoped.row.goods_name }}</td>
        <td>{{ scoped.row.goods_price }}</td>
        <!-- <td>{{scoped.row.tags}}</td> -->
        <td>
          <input
            type="text"
            name=""
            id="scoped.row.id"
            class="tag-input form-control"
            v-if="scoped.row.inputVisible"
            v-focus
            @blur="scoped.row.inputVisible = false"
            v-model="scoped.row.inputValue"
            @keydown.enter="tagsFn(scoped)"
          />
          <button
            class="btn btn-primary btn-sm add-tag"
            v-else
            @click="scoped.row.inputVisible = true"
          >
            +Tag
          </button>
          <span v-for="(item,index) in scoped.row.tags" :key="index"  class="badge badge-warning">{{ item }}</span>
          <!-- <span
            v-for="(str, ind) in scoped.row.tags"
            :key="ind"
            class="badge badge-warning"
          >
            {{ str }}
          </span> -->
        </td>
        <td>
          <button class="btn btn-danger btn-sm" @click="delFn(scoped.row.id)">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from './MyTable'
export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    }
  },
  created() {
    this.$axios({
      url: '/api/goods',
    }).then((res) => {
      let {
        data: { data },
      } = res
      this.list = data
      console.log(data)
    })
  },
  methods: {
    delFn(id) {
      let index = this.list.findIndex((item) => item.id === id)
      this.list.splice(index, 1)
    },
    tagsFn(scoped){
      if(scoped.row.inputValue.trim()==='') return alert('请输入内容')
    scoped.row.tags.push(scoped.row.inputValue) 
    scoped.row.inputValue=""
    }
  },
  directives: {
    focus: {
      inserted(el) {
        el.focus()
      },
    },
  },
}
</script>
<style scoped>
.badge{
  background-color: rgb(244, 227, 40);
  color: black;

}
span{
  margin: 2px;
}
</style>